package org.apache.servicecomb.samples.practise.houserush.customer.manage.service;

import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.samples.practise.houserush.customer.manage.dao.CustomerDao;
import org.apache.servicecomb.samples.practise.houserush.customer.manage.dao.QualificationDao;
import org.apache.servicecomb.samples.practise.houserush.customer.manage.entity.Customer;
import org.apache.servicecomb.samples.practise.houserush.customer.manage.entity.Qualification;
import org.apache.servicecomb.samples.practise.houserush.customer.manage.rpc.HouseOrderApi;
import org.apache.servicecomb.samples.practise.houserush.customer.manage.rpc.UserApi;
import org.apache.servicecomb.samples.practise.houserush.customer.manage.rpc.po.SaleQualification;
import org.apache.servicecomb.samples.practise.houserush.customer.manage.rpc.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private QualificationDao qualificationDao;

    @RpcReference(microserviceName = "house-order", schemaId = "houseOrderApiRest")
    private HouseOrderApi houseOrderApi;
    @RpcReference(microserviceName = "login", schemaId = "userApi")
    private UserApi userApi;

    @Override
    public List<Customer> indexCustomers() {
        return customerDao.findAll();
    }

    @Override
    public Customer findCustomer(int id) {
        return customerDao.findOne(id);
    }

    @Override
    @Transactional
    public Customer createCustomer(Customer customer) {
        //创建客户的同时，同时创建用户信息用于登录
        User user = new User();
        user.setUsername(customer.getRealName());
        //默认123456，后期客户自己修改
        user.setPassword("123456");
        User user1 = userApi.createUser(user);

        //声明一个保存冗余数据的容器
        List<SaleQualification> saleQualifications = new ArrayList<>();
        //获取客户所具有的活动资格
        //对客户的活动数据进行处理
        List<Qualification> qualifications = customer.getQualifications();
        for (Qualification qualification : qualifications) {
            Customer c = new Customer();
            c.setId(user1.getId());
            qualification.setCustomer(c);

            SaleQualification saleQualification = new SaleQualification();
            saleQualification.setCustomerId(customer.getId());
            saleQualification.setSaleId(qualification.getSaleId());
            int countHouseBySaleId = houseOrderApi.countHouseBySaleId(qualification.getSaleId());
            saleQualification.setQualificationCount(countHouseBySaleId);
            saleQualifications.add(saleQualification);
        }


        //保存用户信息
        customer.setId(user1.getId());
        customerDao.save(customer);


        //在house-order中保存活动资格的冗余数据
        houseOrderApi.updateSaleQualification(saleQualifications);
        return customer;
    }

    @Override
    public Customer updateCustomer(Customer customer) {
        Integer id = customer.getId();
        if (customerDao.exists(id)) {
            houseOrderApi.removeSaleQualification(customer.getId());

            updateCustomerQualification(customer,customer.getQualifications());
            return customer;
        } else {
            throw new DataRetrievalFailureException("不能修改不存在的数据");
        }



    }

    @Transactional
    public  boolean updateCustomerQualification(Customer customer, List<Qualification> qualifications) {
        //给客户设置活动资格数据
        customer.setQualifications(qualifications);

        //给活动资格设置客户信息
        for (Qualification qualification : qualifications) {
            qualification.setCustomer(customer);
        }

        //保存客户数据 级联保存，同时保存customer和qualifications两张表
        customerDao.saveAndFlush(customer);

        //准备haoseorder需要的冗余数据
        List<SaleQualification> saleQualifications = new ArrayList<>();
        for (Qualification qualification : qualifications) {
            SaleQualification saleQualification = new SaleQualification();
            saleQualification.setCustomerId(customer.getId());
            saleQualification.setSaleId(qualification.getSaleId());
            int countHouseBySaleId = houseOrderApi.countHouseBySaleId(qualification.getSaleId());
            saleQualification.setQualificationCount(countHouseBySaleId);
            saleQualifications.add(saleQualification);
        }

        //保存活动资格数据是保存冗余在house order中的sale_qualification这张表
        houseOrderApi.updateSaleQualification(saleQualifications);
        return true;

    }

    @Override
    public void removeCustomer(int id) {
        userApi.removeUser(id);
        customerDao.delete(id);
    }
}
